package com.lagou.server;

import com.lagou.util.MySerializer;
import org.I0Itec.zkclient.ZkClient;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

@SuppressWarnings("all")
public class BIOServer2 {

    public static void main(String[] args) throws IOException {
        //开启监听8081
        ServerSocket serverSocket = new ServerSocket(8081);

        ZkClient zkClient = new ZkClient("101.133.232.19:2181");
        //使用自定义序列化
        zkClient.setZkSerializer(new MySerializer());
        //使用临时顺序性节点注册服务端
        zkClient.createEphemeralSequential("/server-cluster/server-", "8081");

        while (true) {
            Socket socket = serverSocket.accept();
            InputStream inputStream = socket.getInputStream();
            while (true) {
                byte[] bytes = new byte[1024];
                int length;
                if ((length = inputStream.read(bytes)) > 0) {
                    System.out.println(new String(bytes, 0, length));
                }
            }
        }
    }

}
